﻿using System;
using System.Collections.Generic;
using System.Text;
using PPI.Entities.Distribution;
using System.Data;
using PPI.Mobile.Logic;
using VMind.Global;

namespace PPI.Client.Logic.Distribution
{
    public class LMeterNote
    {
        public static List<ENote> SelectAllIdName()
        {
            DataTable dt = WebConsumer.Distribution.WDistributionNotes.SelectAllMeterNotesIdName().Tables[0];
            List<ENote> lstNotes = new List<ENote>();

            foreach (DataRow drNote in dt.Rows)
            {
                ENote note = new ENote();

                note.Id = ConvertHelper.ToInt32(drNote["MeterNoteId"]);
                note.Key = drNote["MeterNoteId"].ToString();
                note.Name = drNote["Name"].ToString();

                lstNotes.Add(note);
            }

            return lstNotes;
        }

        public static List<ENote> Save(List<ENote> meters, Int32 poleId)
        {
            return WebConsumer.Distribution.WPole.SaveMeterNotes(poleId, meters);
        }

        public static void UpdateUploadedData(List<ENote> notes, Int32 meterId, string connectionString)
        {
            foreach (ENote note in notes)
            {
                StringBuilder _query = new StringBuilder();
                _query.AppendLine("UPDATE tb_NotesXMeters set NoteXMeterServerId = " + note.ServerId + ", MeterServerID = " + meterId + ", UploadedDate = " +
                                  DateTime.Now.ToString("yyyy-MM-dd") + " where NoteXMeterId = " + note.Id + " ");

                LMobileDatabase.ExecuteQuery(_query.ToString(), connectionString);
            }
        }

        public static List<ENote> GetMeterNotes(EMeter meter, string connectionString)
        {
            StringBuilder _sql = new StringBuilder();

            #region Query

            _sql.AppendLine("SELECT ");
            _sql.AppendLine("   NXM.NoteXMeterId, ");
            _sql.AppendLine("   NXM.NoteFk, ");
            _sql.AppendLine("   LKMN.LookupText ");
            _sql.AppendLine("FROM tb_NotesXMeters NXM ");
            _sql.AppendLine("LEFT JOIN tb_Lookups LKMN ON LKMN.LookupValue = NXM.NoteFk AND LKMN.LookupTypeFk = 'MeterNote' ");
            _sql.AppendLine("WHERE NXM.MeterFk = " + meter.Id + ";");

            #endregion

            DataTable dt = LMobileDatabase.ExecuteQueryDataset(_sql.ToString(), connectionString).Tables[0];

            List<ENote> lstNotes = new List<ENote>();

            foreach (DataRow dr in dt.Rows)
            {
                ENote meterNote = new ENote();

                meterNote.Id = ConvertHelper.ToInt32(dr["NoteXMeterId"]);
                meterNote.Key = dr["NoteFk"].ToString();
                meterNote.Name = dr["LookupText"].ToString();

                lstNotes.Add(meterNote);
            }

            return lstNotes;
        }

        public static List<ENote> GetProjectMeterNotes(EMeter meter, string connectionString)
        {
            StringBuilder _sql = new StringBuilder();

            #region Query

            _sql.AppendLine("SELECT ");
            _sql.AppendLine("   NXM.NoteXMeterId, ");
            _sql.AppendLine("   NXM.NoteFk, ");
            _sql.AppendLine("   LKMN.LookupText ");
            _sql.AppendLine("FROM tb_NotesXMeters NXM ");
            _sql.AppendLine("LEFT JOIN tb_Lookups LKMN ON LKMN.LookupValue = NXM.NoteFk AND LKMN.LookupTypeFk = 'MeterNote' ");
            _sql.AppendLine("WHERE NXM.MeterFk = " + meter.Id + ";");

            #endregion

            DataTable dt = LMobileDatabase.ExecuteQueryDataset(_sql.ToString(), connectionString).Tables[0];

            List<ENote> lstNotes = new List<ENote>();

            foreach (DataRow dr in dt.Rows)
            {
                ENote meterNote = new ENote();

                meterNote.Id = ConvertHelper.ToInt32(dr["NoteXMeterId"]);
                meterNote.Key = dr["NoteFk"].ToString();
                meterNote.Name = dr["LookupText"].ToString();

                lstNotes.Add(meterNote);
            }

            return lstNotes;
        }
    }
}
